package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.RiJiAdapter;
import com.xgkj.diyiketang.data.PengYouQuanInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.HisPengYouQuanListTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.weidget.NoScrollGridView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/7/27.
 */
public class MyRiJiActivity extends BaseActivity implements MyScrollView.OnScrollListener {
    private Context mContext;
    private NoScrollGridView mGridView;
    private RiJiAdapter mAdapter;
    private RelativeLayout mTopLayout;
    private MyScrollView mScrollView;
    private int currentPage = 1;
    private int size = 10;
    private int anchorListTaskId = -1;
    private List<PengYouQuanInfo> mList = new ArrayList<PengYouQuanInfo>();
    private ImageView mUserLogo;
    private TextView mUserNick;

    private String mUserId;
    private String nickName;
    private String userLogo;
    private TextView mTitle;
    private TextView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myriji);
        mContext = this;
        initView();
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle = (TextView) findViewById(R.id.title);
        Intent intent = getIntent();
        if (intent != null){
            mUserId = intent.getStringExtra("USERID");
            nickName = intent.getStringExtra("NICKNAME");
            userLogo = intent.getStringExtra("HEADLOGO");
        }

        if (mUserId == null){
            mTitle.setText("我的日记");
            mUserId = "";
        }else {
            mTitle.setText("ta的日记");
        }

        if (nickName == null){
            nickName = EatShowApp.getInstance().getmUserInfo().getNickName();
        }

        if (userLogo == null){
            userLogo = EatShowApp.getInstance().getmUserInfo().getUserLogo();
        }
        mTopLayout = (RelativeLayout) findViewById(R.id.toplayout);
        mTopLayout.setFocusable(true);
        mTopLayout.setFocusableInTouchMode(true);
        mTopLayout.requestFocus();
        mGridView = (NoScrollGridView) findViewById(R.id.riji_list);
        mAdapter = new RiJiAdapter(mContext);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent detailIntent = new Intent(mContext, PengYouQuanDetailActivity.class);
                detailIntent.putExtra("INFO", (PengYouQuanInfo) mAdapter.getItem(position));
                startActivity(detailIntent);
            }
        });

        mScrollView = (MyScrollView) findViewById(R.id.scrollView);
        mScrollView.setOnScrollListener(this);
        mUserLogo = (ImageView) findViewById(R.id.head_img);
        mUserNick = (TextView) findViewById(R.id.nickname);
//        String headImgUrl = EatShowApp.getInstance().getmUserInfo().getUserLogo();
        if (userLogo != null && userLogo.length() > 0) {
            ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
//            Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(userLogo, size));
//            if (cacheBitmap != null) {
//                mUserLogo.setImageBitmap(cacheBitmap);
//            } else {
                mUserLogo.setImageResource(R.drawable.default_head_image);
                ImageCache.getInstance(mContext).loadBitmapToView(mUserLogo, userLogo, size);
//            }

        } else {
            mUserLogo.setImageResource(R.drawable.default_head_image);
        }

        if (nickName != null) {
            try {
                mUserNick.setText(URLDecoder.decode(nickName, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        getData();
    }


    private void getData() {
        if (currentPage == 0) {
            //Toast.makeText(mContext, R.string.no_more, Toast.LENGTH_SHORT).show();
            return;
        }
        if (anchorListTaskId != -1) {
            return;
        }
        HisPengYouQuanListTask task = new HisPengYouQuanListTask(size, currentPage, mUserId);
        task.addListener(anchorListListener);
        anchorListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    @Override
    public void onScroll() {
        getData();
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == anchorListTaskId) {
                anchorListTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                }
                if (data != null) {
                    mList = (List<PengYouQuanInfo>) data;
                    if (mList != null && mList.size() > 0) {
                        mAdapter.updateData(mList);
                    }
                }

                currentPage = result;
                if (currentPage == 0) {

                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == anchorListTaskId) {
                anchorListTaskId = -1;
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {

        }
    };
}
